package team.bailan.common.process.ingame;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import team.bailan.common.enums.CardTypeEnum;
import team.bailan.common.process.dealcard.InitialCards;
import team.bailan.common.process.roblord.RobLordMap;
import team.bailan.common.server.WebSocketServer;
import team.bailan.entity.domain.Card;
import team.bailan.entity.domain.CardType;
import team.bailan.entity.domain.State;
import team.bailan.entity.vo.InitVO;
import team.bailan.entity.vo.JudgeCardVO;
import team.bailan.entity.vo.PlayCardInf;
import team.bailan.entity.vo.ResultInfVO;

import java.io.IOException;
import java.util.*;

@Data
public class InGame {

    State nowState;
    int lordPosition;
    Map<String, List<Card>> playerCardsMap;
    boolean isStart;

    public static void init(int roomId) throws IOException {
        if (InitialCards.initialCardsMap.containsKey("room_" + roomId)) {
            InitialCards.initialCardsMap.get("room_" + roomId).init();
            InitialCards.initialCardsMap.get("room_" + roomId).dealCardsProcess();
        }
        if (RobLordMap.robLordMap.containsKey("room_" + roomId)) {
            RobLordMap.robLordMap.get("room_" + roomId).init();
        }
        if (RoomInfMap.roomInfMap.containsKey("room_" + roomId)) {
            RoomInfMap.roomInfMap.get("room_" + roomId).init();
        }
        if (InGameMap.inGameMap.containsKey("room_" + roomId)) {
            InGame inGame = InGameMap.inGameMap.get("room_" + roomId);
            inGame.setNowState(new State(roomId, 0, new CardType(CardTypeEnum.CARD_ARBITRARY, 0, 0), inGame.lordPosition));
            inGame.setStart(false);
        }
        WebSocketServer.webSocketServer.sendToRoom(JSON.toJSONString(
                new InitVO("init", RoomInfMap.roomInfMap.get("room_" + roomId).getPositionFlag(), RoomInfMap.roomInfMap.get("room_" + roomId).getPrepareFlag())), roomId + "");
    }

    public JudgeCardVO judgePlayCard(List<Integer> cardPosition) {
        List<Card> cardList = getCardListByPosition(playerCardsMap.get("player" + nowState.getPlayerPosition()), cardPosition);
        CardType upcomingCardType = Card.getCardType(cardList);
        CardType discardType = nowState.getCardType();
        if (upcomingCardType.cardTypeEnum == CardTypeEnum.CARD_INVALID) {
            return new JudgeCardVO(false, upcomingCardType, discardType);
        }
        return new JudgeCardVO(CardType.judgeCardType(upcomingCardType, discardType), upcomingCardType, discardType);

    }

    public synchronized void playCards(List<Integer> cardPosition) throws IOException {
        List<Card> playerCards = playerCardsMap.get("player" + nowState.getPlayerPosition());
        List<Card> cardList = getCardListByPosition(playerCardsMap.get("player" + nowState.getPlayerPosition()), cardPosition);
        nowState.setCardType(Card.getCardType(cardList));
        /*
            如果是炸弹或者王炸翻倍
         */
        if (nowState.getCardType().cardTypeEnum == CardTypeEnum.CARD_AAAA) {
            if (RoomInfMap.roomInfMap.containsKey("room_" + nowState.getRoomId())) {
                System.out.println("7777777777");
                RoomInfMap.roomInfMap.get("room_" + nowState.getRoomId()).getFractionCalculator().redouble();
            }
        }
        nowState.setLastPlayerPosition(nowState.getPlayerPosition());
        int nextPosition = nowState.getPlayerPosition() % 3 + 1;
        playerCards.removeAll(cardList);
        playerCardsMap.put("player" + nowState.getPlayerPosition(), playerCards);
        WebSocketServer.webSocketServer.sendToRoom(JSON.toJSONString(
                new PlayCardInf("PlayCard", true, cardList, nowState.getPlayerPosition(), nextPosition, nowState.getCardType())), nowState.getRoomId() + "");
        if (playerCards.size() == 0) {
            if (nowState.getPlayerPosition() == lordPosition) {
                WebSocketServer.webSocketServer.sendToRoom(JSON.toJSONString(
                        new ResultInfVO("Result", "landlord", lordPosition)), nowState.getRoomId() + "");
                if (RoomInfMap.roomInfMap.containsKey("room_" + nowState.getRoomId())) {
                    RoomInfMap.roomInfMap.get("room_" + nowState.getRoomId()).calculateBeans("landlord", lordPosition);

                }
            } else {
                WebSocketServer.webSocketServer.sendToRoom(JSON.toJSONString(
                        new ResultInfVO("Result", "farmer", nowState.getPlayerPosition())), nowState.getRoomId() + "");
            }
            InGame.init(getNowState().getRoomId());
        }
        nowState.setPlayerPosition(nextPosition);

    }

    public void doNotPlay() throws IOException {
        int nextPosition = nowState.getPlayerPosition() % 3 + 1;
        WebSocketServer.webSocketServer.sendToRoom(JSON.toJSONString(
                new PlayCardInf("PlayCard", false, null, nowState.getPlayerPosition(), nextPosition, null)), nowState.getRoomId() + "");
        nowState.setPlayerPosition(nextPosition);
        if (nowState.getLastPlayerPosition() == nextPosition) {
            nowState.setCardType(new CardType(CardTypeEnum.CARD_ARBITRARY, 0, 0));
        }
    }


    // 以下是一些其他方法
    public List<Card> getCardListByPosition(List<Card> cardList, List<Integer> cardPosition) {
        List<Card> result = new ArrayList<>();
        for (int pos : cardPosition) {
            result.add(cardList.get(pos));
        }
        return result;
    }

    @Override
    public String toString() {
        return "InGame{" +
                "nowState=" + nowState +
                ", lordPosition=" + lordPosition +
                ", playerCardsMap=" + playerCardsMap +
                '}';
    }
}
